package com.timoon.component.panel.bootstrap.modal;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;

import com.timoon.app.utils.GlobalRandom;

/**
 *<p>bootstrap.modal</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public abstract class ModalPanel extends Panel {

	private String htmlID="modal-"+GlobalRandom.getRandom();
	
	private String title="default-title";
	
	private String btnTEXT="ACTION";
	
	private String content;
	
	/**
	 * body面板从getBodyPanel()获取
	 * @param id
	 * @param title
	 * @param btnText
	 */
	public ModalPanel(String id,String title,String btnText) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.title=title;
		
		this.btnTEXT=btnText;
		
		renderPanel();
	}
	
	/**
	 * body面板显示content
	 * @param id
	 * @param title
	 * @param btnText
	 * @param content
	 */
	public ModalPanel(String id,String title,String btnText,String content) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.title=title;
		
		this.btnTEXT=btnText;
		
		this.content=content;
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderModal();
	}

	private void renderModal() {
		// TODO Auto-generated method stub
		
		WebMarkupContainer modal=new WebMarkupContainer("wk_modal");
		modal.add(new AttributeModifier("id", htmlID));
		add(modal);
		
		renderTitle(modal);
		
		renderBody(modal);
		
		renderAction(modal);
	}

	private void renderAction(WebMarkupContainer modal) {
		// TODO Auto-generated method stub
		Form form=new Form("wk_form");
		modal.add(form);
		
		AjaxButton button=new AjaxButton("wk_action") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				if(onAction(target)){
					target.appendJavaScript("$('#"+htmlID+"').modal('hide')");
					afterCloseWin(target);
				}
			}
		};
		form.add(button);
		
		button.add(new Label("wk_btn_text", btnTEXT));
	}
	
	/**
	 * 激发显示窗口
	 * @param target
	 */
	public void showModal(AjaxRequestTarget target){
		target.appendJavaScript("$('#"+htmlID+"').modal()");
	}

	private void renderBody(WebMarkupContainer modal) {
		// TODO Auto-generated method stub
		
		if(this.content==null){
			//如果content为空，则填充面板
			modal.add(new AjaxLazyLoadPanel("wk_body") {
				
				@Override
				public Component getLazyLoadComponent(String arg0) {
					// TODO Auto-generated method stub
					return getBodyPanel(arg0);
				}
			});
		}else{
			//填充content
			modal.add(new Label("wk_body", content));
		}
		
		
	}

	private void renderTitle(WebMarkupContainer modal) {
		// TODO Auto-generated method stub
		modal.add(new Label("wk_title",this.title));
	}
	public Panel getBodyPanel(String wkID){return null;};
	
	/**
	 * 点击定制按钮后激发
	 * @param target
	 * @return 是否关闭窗口
	 */
	public boolean onAction(AjaxRequestTarget target){return true;};
	
	/**
	 * 点击定制按钮后关闭窗口后激发，只针对定制按钮
	 * @param target
	 */
	public void afterCloseWin(AjaxRequestTarget target){};

}
